package com.sunlee.common.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.utils.ConvertUtils;
import com.sunlee.common.core.utils.CustomizePageUtil;
import com.sunlee.common.core.utils.TreeUtils;
import com.sunlee.common.service.dao.ThirdCategoryDao;
import com.sunlee.common.service.dto.CategoryDTO;
import com.sunlee.common.service.dto.ThirdCategoryDTO;
import com.sunlee.common.service.entity.CategoryEntity;
import com.sunlee.common.service.entity.ThirdCategoryEntity;
import com.sunlee.common.service.entity.ThirdProductEntity;
import com.sunlee.common.service.service.ThirdCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 第三方PPT分类表
 *
 * @author sunlee
 */
@Service
public class ThirdCategoryServiceImpl extends CrudServiceImpl<ThirdCategoryDao, ThirdCategoryEntity, ThirdCategoryDTO> implements ThirdCategoryService {
    @Override
    public PageData<ThirdCategoryDTO> pageOwn(Map<String, Object> params) {
        // 查询所有分类
        List<ThirdCategoryEntity> allEntityList = baseDao.selectList(getWrapper(params));

        // 补全父分类
        addParentCategory(allEntityList);

        //转为树形结构
        List<ThirdCategoryDTO> allDtoList = ConvertUtils.sourceToTarget(allEntityList, ThirdCategoryDTO.class);
        List<ThirdCategoryDTO> treeDtoList = TreeUtils.build(allDtoList, Constant.MENU_ROOT);

        //分页参数
        Long curPage = Long.parseLong((String) params.get(Constant.PAGE));
        Long limit = Long.parseLong((String) params.get(Constant.LIMIT));
        //构建分页
        return CustomizePageUtil.startPage(treeDtoList, curPage, limit);
    }

    /**
     * 补全父分类
     */
    private void addParentCategory(List<ThirdCategoryEntity> allDtoList) {
        // 子类所需的父分类Id
        Set<Long> pId = allDtoList.stream()
                .filter(item -> item.getLevel() == 2)
                .map(ThirdCategoryEntity::getPid)
                .collect(Collectors.toSet());

        // 已有的父分类Id
        Set<Long> existPid = allDtoList.stream()
                .filter(item -> item.getLevel() == 1)
                .map(ThirdCategoryEntity::getId)
                .collect(Collectors.toSet());

        pId.removeAll(existPid);

        if (CollectionUtils.isEmpty(pId)) {
            return;
        }

        // 查询对应父分类
        List<ThirdCategoryEntity> newParentCategoryList = list(new LambdaQueryWrapper<ThirdCategoryEntity>()
                .in(ThirdCategoryEntity::getId, pId));

        allDtoList.addAll(newParentCategoryList);
    }

    @Override
    public LambdaQueryWrapper<ThirdCategoryEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String source = (String) params.get("source");
        String status = (String) params.get("status");

        return new LambdaQueryWrapper<ThirdCategoryEntity>()
                .eq(StringUtils.isNotBlank(id), ThirdCategoryEntity::getId, id)
                .or()
                .like(StringUtils.isNotBlank(id), ThirdCategoryEntity::getCategoryId, id)
                .eq(StringUtils.isNotBlank(source), ThirdCategoryEntity::getSource, source)
                .eq(StringUtils.isNotBlank(status), ThirdCategoryEntity::getStatus, status)
                .orderByAsc(ThirdCategoryEntity::getSource)
                .orderByDesc(ThirdCategoryEntity::getCreateDate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOwn(ThirdCategoryDTO dto) {
        if (dto.getPid() == 0L) {
            update(new LambdaUpdateWrapper<ThirdCategoryEntity>()
                    .eq(ThirdCategoryEntity::getPid, dto.getId())
                    .ne(ThirdCategoryEntity::getStatus, dto.getStatus())
                    .set(ThirdCategoryEntity::getStatus, dto.getStatus()));
        }
        super.updateOwn(dto);
    }
}